<?php
/**
 * 2012.7.6
 * @author uther,tjb
 *
 */
class M_tbk extends CI_Model {
	var $iid = '';
	var $title = '';
	var $url = '';
	var $ip = '';
	var $date = '';
	function __construct() {
		// Call the Model constructor
		parent::__construct ();
	}
	public function click($num_iid) {
		$res = $this->db->get_where ( 'click', array (
				'num_iid' => $num_iid 
		) );
		if ($res->num_rows > 0) {
			$sql = "UPDATE `tb_click` SET click=click+1 WHERE num_iid=?";
			$this->db->query ( $sql, $num_iid );
		} else {
			$info = array (
					'num_iid' => $num_iid,
					'click' => 1 
			);
			$this->db->insert ( 'click', $info );
		}
	}
	public function add_good_info($val) {
		$res = $this->db->get_where ( 'good', array (
				'num_iid' => $val ['num_iid'] 
		) );
		if ($res->num_rows == 0) {
			unset ( $val ['click_url'] );
			$this->db->insert ( 'good', $val );
		}
	}
	
	// tjb
	/**
	 * add data
	 *
	 * @param unknown_type $table        	
	 * @param unknown_type $info        	
	 */
	function add_user($data) {
		$res = $this->db->get_where ( 'user', array (
				'usermail' => $data ['usermail'] 
		) );
		if ($res->num_rows == 0) {
			$this->db->insert ( 'user', $data );
			return $this->db->insert_id ();
		} else {
			return false;
		}
	}
	/**
	 * select one
	 *
	 * @param unknown_type $table        	
	 * @param unknown_type $where        	
	 * @return unknown
	 */
	function getOne($table, $where) {
		$this->db->where ( $where );
		$this->db->from ( $table );
		$re = $this->db->get ()->result_array ();
		if ($re)
			return $re [0];
		else {
			return false;
		}
	}
	function tbk_query($sql) {
		$view = $this->db->query ( $sql );
		if ($view->num_rows > 0) {
			return $view->result ();
		} else {
			return false;
		}
	}
	function querys($sql) {
		return $this->db->query ( $sql );
	}
	/**
	 * update datebase
	 */
	function update($table, $data, $where) {
		if ($where == "" || $data == "")
			return;
		return $this->db->update ( $table, $data, $where );
	}
	public function get_info() {
		$sql = "SELECT g.*,c.click FROM `tb_good` AS g LEFT JOIN `tb_click` AS c ON g.num_iid=c.num_iid ORDER BY g.id DESC";
		$res = $this->db->query ( $sql );
		$val = $res->result ();
		return $val;
	}
	/*
	 * get help from datebase help
	 */
	public function get_help() {
		$res = $this->db->query ( "SELECT * FROM `tb_help`" );
		$val = $res->result ();
		$row = array ();
		foreach ( $val as $key ) {
			$row [$key->id] ['id'] = $key->id;
			$row [$key->id] ['item_name'] = $key->item_name;
			$row [$key->id] ['item_content'] = $key->item_content;
		}
		return $row;
	}
	
	/*
	 * rebate model class
	 */
	public function add_rec($res) {
		$this->db->select('rid');
		$this->db->where(array('trade_id'=>$res['trade_id']));
		$res = $this->db->get('rebate');
		if($res){
			return false;
		}else{
			return $this->db->insert ( 'rebate', $res );
		}
	}
	
	/*
	 * 兼容性 添加数据
	 */
	public function add_tjb($table, $data) {
		$res = $this->db->insert ( $table, $data );
		if ($res) {
			return $this->db->insert_id ();
		} else {
			return false;
		}
	}
	public function get_rebate_rec($uip = '', $uid = '') {
		if ($uid) {
			$sql = 'SELECT * FROM `tb_rebate` WHERE can_rebate=1 AND (uip=? or uid=?)';
			$res = $this->db->query ( $sql, array (
					$uip,
					$uid 
			) );
		} else {
			$res = $this->db->query ( "SELECT * FROM `tb_rebate` WHERE can_rebate=1 AND uip='$uip'" );
		}
		if ($res->num_rows > 0) {
			$res = $res->result ();
			foreach ( $res as $key ) {
				$row [$key->rid] ['rid'] = $key->rid;
				$row [$key->rid] ['tao_user'] = $key->tao_user;
				$row [$key->rid] ['rebate_cash'] = $key->rebate_cash;
				$row [$key->rid] ['trade_id'] = $key->trade_id;
				$row [$key->rid] ['can_rebate'] = $key->can_rebate;
				$row [$key->rid] ['is_rebate'] = $key->is_rebate;
				$row [$key->rid] ['submit_date'] = date ( 'Y-m-d d:i:s', $key->submit_date );
				$row [$key->rid] ['rebate_date'] = date ( 'Y-m-d d:i:s', $key->rebate_date );
			}
			return $row;
		} else {
			return false;
		}
	}
	
	/*
	 * admin model class
	 */
	public function add($db = '', $data = '') {
		$res = $this->db->insert ( 'user', $data );
		if ($res) {
			return $this->db->insert_id ();
		} else {
			return false;
		}
	}
	/*
	 * user rebate model class
	 */
	public function rebate_add($db,$data = array()) {
		$res = $this->db->insert ( $db, $data );
		if ($res) {
			return $this->db->insert_id ();
		} else {
			return false;
		}
	}
	
	public function in_rebate($id = '') {
		$sql = "SELECT rebate_cash FROM `tb_rebate` WHERE uid='$id' AND is_rebate=0";
		$res = $this->db->query ( $sql );
		if ($res->num_rows) {
			return $res->result_array ();
		} else {
			return false;
		}
	}
	
	public function email_update($data = '') {
		$user_id = $data ['user_id'];
		$email = $data['email'];
		$sql = "UPDATE `tb_union_users` SET `email`='$email' WHERE `user_id`='$user_id'";
		$res = $this->db->query($sql);
		return $res;
	}
	public function count($sql){
		$res = $this->db->query ( $sql );
		return $res->num_rows;
	}
}
?>